package org.fuys.coder.domain.config.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.fuys.coder.domain.config.model.req.AppConfigAddVersionReq;
import org.fuys.coder.common.exception.BaseException;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.config.service.BaseConfig;
import org.fuys.coder.infrastructure.dao.config.AuditConfigMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.common.config
 * @className: FuysAuditConfig
 * @author: WangJing
 * @description: 审核相关配置
 * @date: 2024/6/22 16:26
 * @version: 1.0
 */
@Configuration
@Component("auditConfig")
@TableName("audit_config")
public class CoderAuditConfig extends BaseConfig implements Serializable {
    //文本审核选用的算法
    private String textService;
    //一次文本审核送检的长度
    private Integer textLength;
    //图片审核需要的线程池数量
    private Integer imageAuditCores;
    //图片审核采用的算法
    private String imageService;
    //每个标签容忍的置信度
    private Float imageConfidence;
    //内容审核的线程池数量
    private Integer substanceAuditCores;
    //内容被举报到需要审核的阈值
    private Integer reportCount;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Resource
    @TableField(exist = false)
    private AuditConfigMapper auditConfigMapper;

    @PostConstruct
    public void initConfigValue(){
        final CoderAuditConfig config = auditConfigMapper.selectOne(new LambdaQueryWrapper<CoderAuditConfig>()
                .eq(CoderAuditConfig::getVersion, 1));
        if(config==null){
            throw new BaseException("audit相关配置注入异常:数据库中不存在相关数据");
        }
        this.updateAllValue(1,config);
    }

    @Override
    protected void setConfigDBValue(Object instance, Integer version) throws RuntimeException {
        if(instance instanceof CoderAuditConfig){
            CoderAuditConfig config=(CoderAuditConfig)instance;
            config.setVersion(version);
            auditConfigMapper.updateById(config);
        }else{
            throw new BaseException("无法同步到数据库 请检查传递的数据格式! --来自审核配置");
        }
    }


    @Override
    protected void switchConfigVersion(Integer version) {
        final CoderAuditConfig config = auditConfigMapper.selectOne(new LambdaQueryWrapper<CoderAuditConfig>()
                .eq(CoderAuditConfig::getVersion, version));
        if(ObjectUtils.isEmpty(config)){
            throw new BusinessException("没有此版本号的配置文件 请核对之后再次切换 或添加新版本 --来自审核配置");
        }
        this.updateAllValue(version,config);
    }

    @Override
    protected void addConfigVersion(AppConfigAddVersionReq req) {
        CoderAuditConfig auditConfig=new CoderAuditConfig();
        auditConfig.setVersion(req.getVersion());
        auditConfig.setDescription(req.getDesc());
        auditConfig.setSubstanceAuditCores(req.getSubstanceAuditCores());
        auditConfig.setImageService(req.getImageService());
        auditConfig.setImageConfidence(req.getImageConfidence());
        auditConfig.setImageAuditCores(req.getImageAuditCores());
        auditConfig.setReportCount(req.getReportCount());
        auditConfig.setTextLength(req.getTextLength());
        auditConfig.setTextService(req.getTextService());
        this.auditConfigMapper.insert(auditConfig);
    }

    @Override
    protected List<? extends BaseConfig> getConfigList() {
        return this.auditConfigMapper.selectList(new LambdaQueryWrapper<>());
    }


    private void updateAllValue(Integer version,CoderAuditConfig config){
        this.setVersion(version);
        this.setTextService(config.getTextService());
        this.setTextLength(config.getTextLength());
        this.setImageAuditCores(config.getImageAuditCores());
        this.setImageConfidence(config.getImageConfidence());
        this.setImageService(config.getImageService());
        this.setReportCount(config.getReportCount());
        this.setSubstanceAuditCores(config.getSubstanceAuditCores());
    }

    public String getTextService() {
        return textService;
    }

    public void setTextService(String textService) {
        this.textService = textService;
    }

    public Integer getTextLength() {
        return textLength;
    }

    public void setTextLength(Integer textLength) {
        this.textLength = textLength;
    }

    public Integer getImageAuditCores() {
        return imageAuditCores;
    }

    public void setImageAuditCores(Integer imageAuditCores) {
        this.imageAuditCores = imageAuditCores;
    }

    public String getImageService() {
        return imageService;
    }

    public void setImageService(String imageService) {
        this.imageService = imageService;
    }

    public Float getImageConfidence() {
        return imageConfidence;
    }

    public void setImageConfidence(Float imageConfidence) {
        this.imageConfidence = imageConfidence;
    }

    public Integer getSubstanceAuditCores() {
        return substanceAuditCores;
    }

    public void setSubstanceAuditCores(Integer substanceAuditCores) {
        this.substanceAuditCores = substanceAuditCores;
    }

    public Integer getReportCount() {
        return reportCount;
    }

    public void setReportCount(Integer reportCount) {
        this.reportCount = reportCount;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CoderAuditConfig)) return false;
        CoderAuditConfig config = (CoderAuditConfig) o;
        return Objects.equals(getTextService(), config.getTextService()) && Objects.equals(getTextLength(), config.getTextLength()) && Objects.equals(getImageAuditCores(),
                config.getImageAuditCores()) && Objects.equals(getImageService(), config.getImageService()) && Objects.equals(getImageConfidence()
                , config.getImageConfidence()) && Objects.equals(getSubstanceAuditCores(), config.getSubstanceAuditCores()) && Objects.equals(getReportCount(), config.getReportCount());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getTextService(), getTextLength(), getImageAuditCores(), getImageService(), getImageConfidence(), getSubstanceAuditCores(), getReportCount());
    }
}
